package com.thwj.project.app.api;

import com.thwj.common.utils.EnumUtil;
import com.thwj.framework.mybatis_plus.GeoUtil;
import com.thwj.framework.web.domain.R;
import com.thwj.project.app.api.vo.EnumVo;
import com.thwj.project.biz.domain.Activity;
import com.thwj.project.biz.service.IActivityService;
import com.thwj.project.monitor.task.JobTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags="app公共模块")
@RestController
@RequestMapping("/app/common")
public class AppCommonController {
    @Autowired
    WxMpService wxMpService;

    @ApiOperation("获取广告banner列表")
    @GetMapping("/getEnumsByPath")
    public R<List<EnumVo>> getEnumsByPath(@RequestParam String classFullName) {
        // 获取满足条件的banner列表
        Map<Integer,String> valueNameMap = EnumUtil.getEnum(classFullName);
        List<EnumVo> enumVoList = new ArrayList<>();
        valueNameMap.forEach((key,value)->{enumVoList.add(new EnumVo(key,value));});
        return R.data(enumVoList);
    }

    @ApiOperation("获取广告banner列表")
    @GetMapping("/getEnumsMapByPath")
    public R<Map<Integer,String>> getEnumsMapByPath(@RequestParam String classFullName) {
        // 获取满足条件的banner列表
        Map<Integer,String> valueNameMap = EnumUtil.getEnum(classFullName);
        return R.data(valueNameMap);
    }


    @Autowired
    IActivityService activityService;

    @ApiOperation("test11")
    @GetMapping("/test11")
    public R<String> test11() {
        //
        List<Activity> activities = activityService.list(null);
        activities.forEach(item->{
            item.setLocation(GeoUtil.getWgs84Point(item.getLatitude(),item.getLongitude()));
        });
        activityService.updateBatchById(activities);

        return R.data("");
    }



    @Autowired
    JobTask jobTask;

    @ApiOperation("testjobTask")
    @GetMapping("/testjobTask")
    public R<String> testjobTask(String endDate) {
        //
        jobTask.activitySignUpSettle2Account(endDate);

        return R.data("");
    }



    @ApiOperation("创建公众号菜单")
    @GetMapping("/gzhMenuCreate")
    public R<String> gzhMenuCreate() {

        WxMenu menu = new WxMenu();
//        WxMenuButton button1 = new WxMenuButton();
//        button1.setType(WxConsts.MenuButtonType.CLICK);
//        button1.setName("今日歌曲");
//        button1.setKey("V1001_TODAY_MUSIC");

        WxMenuButton button1 = new WxMenuButton();
        button1.setType(WxConsts.MenuButtonType.MINIPROGRAM);
        button1.setName("找搭子");
        button1.setAppId("wxed89d1049f7a9fbb");
        // （跳转小程序首页）
        button1.setPagePath("pages/index/index");
//        button1.setUrl("http://mp.weixin.qq.com");

        WxMenuButton button2 = new WxMenuButton();
        button2.setType(WxConsts.MenuButtonType.MINIPROGRAM);
        button2.setName("找活动");
        button2.setAppId("wxed89d1049f7a9fbb");
        // 找活动（跳转小程序派队）
        button2.setPagePath("pages/store/store");
//        button2.setUrl("http://mp.weixin.qq.com");

        WxMenuButton button3 = new WxMenuButton();
        button3.setName("联系我们");

        menu.getButtons().add(button1);
        menu.getButtons().add(button2);
        menu.getButtons().add(button3);


//
        WxMenuButton button31 = new WxMenuButton();
        button31.setType(WxConsts.MenuButtonType.VIEW);
        button31.setName("商务合作");
//        button31.setUrl("https://mp.weixin.qq.com/s?__biz=MzkwMjUwMDE4Mg==&mid=2247483713&idx=1&sn=eb1d5f3339394f8ecbe9904728d0b171&chksm=c0a5db78f7d2526e57caf87c5d1997ebd20a9cb87ae40950be03f1506800343236bc17c5b5b1#rd");

        WxMenuButton button32 = new WxMenuButton();
        button32.setType(WxConsts.MenuButtonType.VIEW);
        button32.setName("官方社群");
//        button32.setUrl("https://mp.weixin.qq.com/s?__biz=MzkwMjUwMDE4Mg==&mid=2247483706&idx=1&sn=0431d3174aa18f90bff806926967e1e1&chksm=c0a5db03f7d252150b3979e2276d03fad2189515710ceafd2436db80ba04947b2319176cfd4c#rd");

        WxMenuButton button33 = new WxMenuButton();
        button33.setType(WxConsts.MenuButtonType.VIEW);
        button33.setName("联系客服");
//        button33.setUrl("https://work.weixin.qq.com/kfid/kfc0c5af571cbb518e3");

        button3.getSubButtons().add(button31);
        button3.getSubButtons().add(button32);
        button3.getSubButtons().add(button33);

        try {
            wxMpService.getMenuService().menuCreate(menu);
        }catch (Exception e){
            log.error("e:{}",e);
        }

        return R.data("");
    }
}
